package jmine.tec.batch.dao;

import jmine.tec.batch.domain.ProcessingUnit;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import bancosys.tec.persist.dao.BaseDAO;

/**
 * @created Feb 6, 2007
 * @author Gustavo Almeida
 */
public class ProcessingUnitDAO extends BaseDAO<ProcessingUnit<?>> {

    /**
     * Conta quantas unidades de processamento do processo estão finalizadas.
     * 
     * @param process o process.
     * @return a quantidade.
     */
    public int countFinished(Process process) {
        Criteria crit = this.getSession().createCriteria(ProcessingUnit.class);
        crit = crit.setProjection(Projections.rowCount());
        crit = crit.add(Restrictions.eq("process", process));
        crit =
                crit.add(Restrictions.or(Restrictions.eq("status", ProcessingUnit.FINISHED),
                        Restrictions.eq("status", ProcessingUnit.ERROR)));
        return (Integer) crit.uniqueResult();
    }
}
